package com.gsbelarus.gedemin.income;

import android.content.Context;

import com.gsbelarus.gedemin.income.adapter.ClientsListAdapter;
import com.gsbelarus.gedemin.income.table.ClientsTable;

import java.util.Calendar;
import java.util.List;

public class ClientsTableHelper {

    private Context context;

    public ClientsTableHelper(Context context) {
        this.context = context;
    }

    public void addTotalSum(List<ClientsTable> contacts, int selectedYear) {
        if (contacts.isEmpty()) return;
        Calendar cal = Calendar.getInstance();
        int curYear = cal.get(Calendar.YEAR);

        int curMonth = (selectedYear == curYear) ? cal.get(Calendar.MONTH) + 1 : (selectedYear < curYear) ? 13 : 0;

        long totalPaidSum = 0;
        long totalPlanSum = 0;
        int position = 0;
        for (int i = 0; i < contacts.size(); i++) {
            if (contacts.get(i).getId() < curMonth) {
                position = i + 1;
                totalPaidSum += contacts.get(i).getPaidSum();
                totalPlanSum += contacts.get(i).getPlanSum();
            } else {
                if (contacts.get(i).getPlanSum() > 0)
                    contacts.get(i).setRenderingMod(ClientsListAdapter.PLAN_WITH_NULL_PAID);
                else
                    contacts.get(i).setRenderingMod(ClientsListAdapter.ALL_INVISIBLE);
                if (contacts.get(i).getId() == cal.get(Calendar.MONTH) + 1 && selectedYear == cal.get(Calendar.YEAR))
                    contacts.get(i).setRenderingMod(ClientsListAdapter.ALL_VISIBLE);
            }
        }
        if (position == 0) return;

        if (selectedYear <= curYear)
            contacts.add(position, new ClientsTable(ClientsListAdapter.TOTAL_SUM, context.getString(R.string.total_sum), totalPaidSum, totalPlanSum));
    }

    public void addTotalSum(List<ClientsTable> contacts, int selectedMonth, int selectedYear) {
        if (contacts.isEmpty()) return;
        Calendar selectedDate = Calendar.getInstance();
        selectedDate.set(Calendar.YEAR, selectedYear);
        selectedDate.set(Calendar.MONTH, selectedMonth - 1);

        Calendar cal = Calendar.getInstance();
        int curYear = cal.get(Calendar.YEAR);
        int curMonth = cal.get(Calendar.MONTH) + 1;
        int curDay;

        if (selectedYear == curYear) {
            if (selectedMonth == curMonth)
                curDay = cal.get(Calendar.DAY_OF_MONTH);
            else if (selectedMonth < curMonth)
                curDay = selectedDate.getActualMaximum(Calendar.DAY_OF_MONTH) + 1;
            else
                curDay = 0;
        } else {
            if (selectedYear < curYear)
                curDay = selectedDate.getActualMaximum(Calendar.DAY_OF_MONTH) + 1;
            else
                curDay = 0;
        }

        long totalPaidSum = 0;
        long totalPlanSum = 0;
        int position = 0;
        for (int i = 0; i < contacts.size(); i++) {
            if (contacts.get(i).getId() < curDay) {
                position = i + 1;
                totalPaidSum += contacts.get(i).getPaidSum();
                totalPlanSum += contacts.get(i).getPlanSum();
            } else {
                if (contacts.get(i).getPlanSum() > 0)
                    contacts.get(i).setRenderingMod(ClientsListAdapter.PLAN_WITH_NULL_PAID);
                else
                    contacts.get(i).setRenderingMod(ClientsListAdapter.ALL_INVISIBLE);
                if (contacts.get(i).getId() == cal.get(Calendar.DAY_OF_MONTH) && selectedMonth == cal.get(Calendar.MONTH) + 1 &&
                        selectedYear == cal.get(Calendar.YEAR))
                    contacts.get(i).setRenderingMod(ClientsListAdapter.ALL_VISIBLE);
            }
        }
        if (position == 0) return;

        if (selectedDate.getTimeInMillis() <= cal.getTimeInMillis())
            contacts.add(position, new ClientsTable(ClientsListAdapter.TOTAL_SUM, context.getString(R.string.total_sum), totalPaidSum, totalPlanSum));
    }
}
